package com.zzs.client.controller;

import com.zzs.client.pojo.bo.user.UpdateInfoBO;
import com.zzs.client.pojo.dto.info.CodeVerifyDTO;
import com.zzs.client.pojo.dto.info.UpdatePasswordDTO;
import com.zzs.client.pojo.dto.user.UpdateInfoDTO;
import com.zzs.client.pojo.vo.user.UserDetailVO;
import com.zzs.client.service.UserInfoService;
import com.zzs.common.core.enums.ResponseCode;
import com.zzs.common.core.response.Result;
import com.zzs.common.core.util.ThreadLocalUtil;
import com.zzs.common.minio.entity.File;
import com.zzs.common.security.exception.ServiceException;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;

/**
 * @author zzs
 * @date 2025/03/26
 */
@RestController
@RequestMapping("/client/user/info")
public class UserInfoController {
    @Autowired
    private UserInfoService userInfoService;

    @GetMapping
    public Result<UserDetailVO> getUserDetail() {
        Long userId = ThreadLocalUtil.getUserId();
        if (userId == null) {
            throw new ServiceException(ResponseCode.UNAUTHORIZED);
        }
        UserDetailVO userDetail = userInfoService.getUserDetail(userId);
        return Result.success(userDetail);
    }

    @PostMapping("/avatar")
    public Result<File> updateUserAvatar(@RequestParam("file") MultipartFile file) {
        File avatarFile = userInfoService.updateUserAvatar(file);
        return Result.success(avatarFile);
    }

    @PutMapping
    public Result<Void> updateUserInfo(@Valid @RequestBody UpdateInfoDTO updateInfoDTO) {
        Long userId = ThreadLocalUtil.getUserId();
        if (userId == null) {
            throw new ServiceException(ResponseCode.UNAUTHORIZED);
        }
        UpdateInfoBO updateInfoBO = UpdateInfoBO.builder()
                .id(userId)
                .username(updateInfoDTO.getUsername().trim())
                .updateTime(LocalDateTime.now())
                .build();
        userInfoService.updateUserInfo(updateInfoBO);
        return Result.success();
    }

    @GetMapping("/sms/old/{phone}")
    public Result<Void> sendCodeToOldPhone(@PathVariable("phone") String phone) {
        userInfoService.sendCodeToOldPhone(phone);
        return Result.success();
    }

    @PostMapping("/sms/old/verify")
    public Result<Void> verifyOldPhone(@RequestBody @Valid CodeVerifyDTO codeVerifyDTO) {
        userInfoService.verifyOldPhone(codeVerifyDTO.getPhone(), codeVerifyDTO.getCode());
        return Result.success();
    }

    @GetMapping("/sms/new/{phone}")
    public Result<Void> sendCodeToNewPhone(@PathVariable("phone") String phone) {
        userInfoService.sendCodeToNewPhone(phone);
        return Result.success();
    }

    @PostMapping("/sms/new/verify")
    public Result<Void> verifyNewPhone(@RequestBody @Valid CodeVerifyDTO codeVerifyDTO) {
        userInfoService.verifyNewPhone(codeVerifyDTO.getPhone(), codeVerifyDTO.getCode());
        return Result.success();
    }

    @GetMapping("/password/{phone}")
    public Result<Void> sendCodeToUpdatePassword(@PathVariable("phone") String phone) {
        userInfoService.sendCodeToUpdatePassword(phone);
        return Result.success();
    }

    @PostMapping("/password")
    public Result<Void> updatePassword(@RequestBody @Valid UpdatePasswordDTO updatePasswordDTO) {
        userInfoService.updatePassword(updatePasswordDTO.getPhone(), updatePasswordDTO.getCode(), updatePasswordDTO.getPassword(), updatePasswordDTO.getPasswordRepeat());
        return Result.success();
    }
}
